package com.googlecode.animator.view.timeline
{
	import flash.events.Event;
	
	import mx.containers.Canvas;
	import mx.core.BitmapAsset;
	
	/**
	 * Covers an area of the timeline to indicate a region wherein something may happen. 
	 * @author USER
	 * 
	 */
	public class SplitMarker extends Marker
	{
		private var _strip:Canvas;
		// private var _begin:SplitMarkerBegin;
		private var _end:SplitMarkerEnd;
		private var _width:uint;
		private var _height:uint;
		private var _markerW:uint;
		
		[Embed(source="com/googlecode/animator/assets/splitMarker.gif")]
		[Bindable]
		private var _splitMarkerImg:Class;
		
		/**
		 * Constructor. 
		 * @param w
		 * @param h
		 * 
		 */
		public function SplitMarker(w:uint, h:uint)
		{
			super(w);
			
			_width = w;
			_height = h;
			
			_end = new SplitMarkerEnd(w);
			
			_strip = new Canvas();
			_strip.setStyle("backgroundColor", 0xff0000);
			_strip.height = h * 0.7;
			_strip.width = w;
			_strip.y = (h - (h * 0.7)) / 2;
			
			var icon:BitmapAsset = new _splitMarkerImg();
			
			_markerW = Math.round(w /3);
			icon.width = _markerW;
			icon.height = w;
			
			
			
			addChild(_strip);
			
			addChild(icon);
			
			addChild(_end);
			
			addEventListener(SnapDraggableObject.OBJECT_DOWN, onObjectDown);
			addEventListener(SnapDraggableObject.OBJECT_UP, onObjectUp);
			addEventListener(SnapDraggableObject.OBJECT_MOVE, onObjectMove);
		}
		
		protected function onObjectDown(evt:Event):void
		{
			if(evt.target is SplitMarkerEnd)
			{
				isDraggable = false;
			}
		}
		
		protected function onObjectUp(evt:Event):void
		{
			isDraggable = true;
		}
		
		protected function onObjectMove(evt:Event):void
		{
			
			if(evt.target is SplitMarkerEnd)
			{
				_strip.width = (_end.time - time ) * _width + _width;
			}
		}
		
		public function set begin(time:int):void
		{
			this.time = time;
		}
		
		public function get begin():int
		{
			return this.time;
		}
		
		public function set end(time:int):void
		{
			_end.time = time;
			
		}
	
		public function get end():int
		{
			return _end.time;
		}
		
		public function isUnder(t:int):Boolean
		{
			if(t >= time && t <= _end.time)
			{
				return true;
			}
			
			return false;
		}
		
		public function stopDragging():void
		{
			onMouseUpAnywhere();
			_end.isDraggable = false;
		}
		
		override public function cleanUp():void
		{
			super.cleanUp();
			
			removeEventListener(SnapDraggableObject.OBJECT_DOWN, onObjectDown);
			removeEventListener(SnapDraggableObject.OBJECT_UP, onObjectUp);
			removeEventListener(SnapDraggableObject.OBJECT_MOVE, onObjectMove);
			
		}
		
		override protected function commitProperties():void
		{
			super.commitProperties();
			
			_strip.width = (_end.time - time ) * _width + _width;
			
			
		}
		override protected function childrenCreated():void
		{
			super.childrenCreated();
			
			_end.container = parent;
		}
	}
}